﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Globalization;
using System.Collections;

namespace SkeletonNET.Localizer.Tests.CustomResourceProvider
{
    public class DatabaseResourceReader : IResourceReader
    {
        public CultureInfo Culture { get; set; }

        public void Close()
        {
            throw new NotImplementedException();
        }

        public DatabaseResourceReader(CultureInfo culture)
        {
            this.Culture = culture;
        }

        public System.Collections.IDictionaryEnumerator GetEnumerator()
        {
            Hashtable table = new Hashtable();
            table.Add("test", "test123");

            return table.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }

    public class CustomResourceSet : ResourceSet
    {
        private readonly CultureInfo culture;

        public CustomResourceSet(CultureInfo culture) : base(new DatabaseResourceReader(culture))
        {
            this.culture = culture;
        }

        public override Type GetDefaultReader()
        {
            return typeof(DatabaseResourceReader);
        }
    }

    public class CustomResourceManager : ResourceManager
    {
        public CustomResourceManager()
        {
            ResourceSets = new Hashtable();
        }

        protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
        {
            CustomResourceSet rs = null;

            if (ResourceSets.Contains(culture.Name))
            {
                rs = ResourceSets[culture.Name] as CustomResourceSet;
            }
            else
            {
                rs = new CustomResourceSet(culture);
                ResourceSets.Add(culture.Name, rs);
            }
            return rs; 
        }
    }
}
